// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    tablist:[
      "推荐","穿搭","美食","彩妆","影视","职场","情感","家居","游戏","萌宠"
    ],
    allNoramalData: [
      {
        id:"101",
        cover: "/images/sucai/_1_王小二cy_来自小红书网页版.jpg",
        title: "#赣州马拉松 #显眼包赣州马拉松的显眼包们 哈哈哈哈哈",
        tx: "/images/sucai/头像.webp",
        name: "王小二cy",
        num: "1000000",
        islike: 1,
        tran_num: "",
        tag: ["穿搭", "推荐"]
      },
      {
        id:"100",
        cover: "/images/sucai/物理老师打篮球一百个只进了三个_1_蛤？_来自小红书网页版.jpg",
        title: "物理老师打篮球一百个只进了三个逊爆了还ikun，但是长的真的很帅#物理老师#坤坤",
        tx: "/images/sucai/1040g2jo30rk4cc0aiq6g5o3altr08utqq3qokq0.webp",
        name: "ha",
        num: "178",
        islike: 1,
        tran_num: "",
        tag: ["穿搭", "职场", "推荐"]
      },
      {
        id:"102",
        cover: "/images/sucai/_1_小红薯673D6FB4_来自小红书网页版 (1).jpg",
        title: "",
        tx: "/images/sucai/645b807d338379fac18930a8.png",
        name: "小红薯673D6FB4",
        num: "888",
        islike: 0,
        tran_num: "",
        tag: ["穿搭", "情感", "推荐"]
      },
      {
        id:"105",
        cover: "/images/sucai/小猫 你是最可爱最可爱的小猫_1_番茄班比_来自小红书网页版.jpg",
        title: "小猫 你是最可爱最可爱的小猫#大学里的猫 #学校的小猫咪 #高校里的猫 #学校的猫猫 #橘猫日常 #橘猫还是那个橘猫",
        tx: "/images/sucai/66111286fbb2b407c4d84c0c.webp",
        name: "番茄班比",
        num: "178",
        islike: 1,
        tran_num: "",
        tag: ["萌宠", "推荐"]
      }  
    ]
  },
  onLoad(e){
    this.countNum()
  },

  onShow(){
    this.countNum()
    let tag
    tag = this.data.tablist[this.data.TabCur]
    let list = this.data.allNoramalData
    let datalist = []
    for(let i = 0;i<list.length;i++){
      if (list[i].tag.indexOf(tag) !== -1) {
        datalist.push(list[i])
      }
    }
    this.setData({
      noramalData:datalist
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    this.onShow()
  },
  toLike(e){
      let id = e.currentTarget.dataset.id
      let list = this.data.noramalData
      list.forEach(element => {
        if(element.id == id){
          if (element.islike == 1) {
            element.islike = 0
            element.num -= 1
          }else{
            element.islike = 1
            element.num += 1
          }
        }
      this.onShow()
      });
  },
  countNum(e){
    let list = this.data.allNoramalData
    for(let i = 0;i<list.length;i++) {
      //10000 => 1.0万
      let num = list[i].num
      if (list[i].num>=10000) {
        num = (num / 10000).toFixed(1) + "万"
      }
      list[i].tran_num = num
    }
    this.setData({
      allNoramalData:list
    })
  },
  toSearch(){
    let user = wx.getStorageSync('userInfo')
    if(user ==  ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      }else{
        wx.navigateTo({
          url: '/pages/search/search',
        })
    }
  }
})